package com.chookss.view;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chookss.Urls;
import com.chookss.tools.FileUtilss;
import com.chookss.tools.Utils;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.io.File;

public class WaterMarkBg extends Drawable {
    private Paint mPaint;
    private Paint mPaintColor;
    private Paint mPaintColor2;
    private Paint mPaintLine;
    private String mWaterStr;
    private Context mContext;
    private String companyName;
    private boolean isPadFlavors = false; //false 小视频黑底专用  true 白底用
    private Bitmap mBitmap;

    public WaterMarkBg(@NonNull Context context, @NonNull String waterStr, String companyName, @NonNull boolean isPadFlavors) {
        this.mContext = context;
        this.companyName = companyName;
        this.mWaterStr = waterStr;
        this.mPaint = new Paint();
        this.mPaintColor = new Paint();
        mPaintColor2 = new Paint();
        mPaintLine = new Paint();
        this.isPadFlavors = isPadFlavors;
        this.mBitmap = null;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        if (TextUtils.isEmpty(mWaterStr)) {
            return;
        }

        int width = getBounds().right;
        int height = getBounds().bottom;
        int length = UIUtil.dip2px(mContext, 18);

        if (isPadFlavors) {
            mPaintColor.setColor(Color.parseColor("#1f000000"));
            mPaintColor2.setColor(Color.parseColor("#1f000000"));
        } else {
            mPaintColor.setColor(Color.parseColor("#3fffffff"));
            mPaintColor2.setColor(Color.parseColor("#3fffffff"));
        }
        if (mBitmap == null) {
            if (isPadFlavors) {
                mBitmap = FileUtilss.getDiskBitmap(mContext, mContext.getExternalFilesDir(Urls.IMAGEFOLDER).getAbsolutePath() + File.separator + "watermarkBlack.jpg");
            } else {
                mBitmap = FileUtilss.getDiskBitmap(mContext, mContext.getExternalFilesDir(Urls.IMAGEFOLDER).getAbsolutePath() + File.separator + "watermark.jpg");
            }
        }
        canvas.save();
        mPaintColor.setAntiAlias(true);
        mPaintColor.setTextSize(length);
        mPaintColor2.setTextSize(length); // ConvertUtils.spToPx()这个方法是将sp转换成px，ConvertUtils这个工具类在我提供的demo里面有
        mPaintColor2.setAntiAlias(true);
        if (isPadFlavors) {
            mPaintLine.setColor(Color.parseColor("#1f000000"));
        } else {
            mPaintLine.setColor(Color.parseColor("#3fffffff"));
        }
        mPaintLine.setStrokeWidth((float) Utils.dip2px(mContext, (float) 1));
        float textWidth = mPaintColor.measureText(mWaterStr);
        mPaintColor.setTextAlign(Paint.Align.CENTER);
        canvas.rotate(-45);
        canvas.drawColor(0x00000000);
        int companyNameWidth = (int) (length * companyName.length());

        int space = Utils.dip2px(mContext, 2);
        int diagonal = (int) Math.sqrt(width * width + height * height); // 对角线的长度
        // 以对角线的长度来做高度，这样可以保证竖屏和横屏整个屏幕都能布满水印
        for (int i = 0; i < 2; i++) {
            int positionX=0,positionY=0;
            if(i==0){
                positionY=diagonal/6;
                positionX = (int) (-textWidth/3); // 上下两行的X轴起始点不一样，错开显示
            }else{
                positionY=2*diagonal/3;
                positionX = (int) (-width + textWidth*4/3); // 上下两行的X轴起始点不一样，错开显示
            }
            if (mBitmap == null) {
                canvas.drawText(companyName, positionX, positionY, mPaintColor2);
                canvas.drawLine(positionX - length, positionY + length * 2 / 3, positionX + companyNameWidth + length, positionY + length * 2 / 3, mPaintLine);
                canvas.drawText(mWaterStr, positionX + companyNameWidth / 2, positionY + length * 2, mPaintColor);
            } else {
                canvas.drawBitmap(mBitmap, positionX, positionY, mPaint);
                canvas.drawText(companyName, positionX + mBitmap.getWidth(), positionY + mBitmap.getHeight() * 3 / 4 + space, mPaintColor2);
                canvas.drawLine(positionX - length / 2, positionY + mBitmap.getHeight() * 7 / 5, positionX + mBitmap.getWidth() + companyNameWidth + length / 2, positionY + mBitmap.getHeight() * 7 / 5, mPaintLine);
                canvas.drawText(mWaterStr, positionX + (mBitmap.getWidth() + companyNameWidth + space) / 2, positionY + mBitmap.getHeight() * 13 / 5, mPaintColor);
            }
        }
        canvas.save();
        canvas.restore();
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }

    /**
     * 设置水印
     *
     * @param context  context
     * @param rootView rootView
     * @param waterTxt waterTxt
     */
    public static void setWaterBg(@NonNull Context context, @NonNull View
            rootView, @NonNull String waterTxt, String String, @NonNull boolean isPadFlavors) {
        rootView.setBackground(new WaterMarkBg(context, waterTxt, String, isPadFlavors));
    }

}
